<?php

/**
 * Aurorum - Open source digital signage
 *
 * @link http://digitalsignage.tk
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Aurorum
 * @package Aurorum
 */

/**
 * This class handles Dashboard requests.
 *
 * @package Aurorum
 * @subpackage Controllers
 */

class ADS_DashboardController extends ADS_DefaultController{
    
    const STATUS_COMMIT_OK = 1;
    
    public function handleDefaultAction(){
        //get variables
        $page     = (int)ADS_Input::getVar('paged', 1);     
        $position = (int)ADS_Input::getVar('position', 1);         
        //load dao
        $chModel = new ADS_ChangelogModel();
        $sModel  = new ADS_SlideModel();
        $siModel = new ADS_SlideItemModel();        
        //process presentation preview
        $descriptor = ads_get_template_descriptor();        
        //load current slide
        $slide  = $sModel->getSlideWithPosition($position);
        $templateInfo = ads_get_template_info(
            get_post_meta($slide->postId, '_wp_page_template', true)
        );
        $scale = 640/$descriptor['size'][0];
        $preview = array(
            'background' => $templateInfo['bg_preview'],
            'frames'     => array(),
            'height'     => round($scale*$descriptor['size'][1])
        );
        $frames = $siModel->getItems((int)$slide->slideId);        
        foreach ($frames as $frame) {
            if(!isset($templateInfo['zones'][$frame->zoneId])){
                continue;
            }
            $zone = $templateInfo['zones'][$frame->zoneId];
            $preview['frames'][] = array(
                'frameId' => $frame->frameId,
                'dimensions' => array(
                    'position' => array(
                        round($zone['dimensions']['position'][0]*$scale),
                        round($zone['dimensions']['position'][1]*$scale)
                    ),
                    'size' => array(
                        round($zone['dimensions']['size'][0]*$scale),
                        round($zone['dimensions']['size'][1]*$scale)
                    ),
                )
            );
        }
//       ads_debug($preview);
        //count records
        $cnt = (int)$chModel->setFilter(array(
            array('status = %i', 0)
        ))->countChanges();
        //load changes
        $changes =  $chModel->setFilter(array(
            array('status = %i', 0)
        ))->setSorter(array(
            'changeId' => 'DESC'
        ))->setPager(
            $page, 5
        )->getChanges();
        //prepare output
        ADS_Output::register('changes', $changes);        
        ADS_Output::register('preview', $preview);        
        ADS_Output::register('slide', $slide);               
        ADS_Output::register('maxPosition', $sModel->countSlides());        
        ADS_Output::register('framePager', $framePager);        
        ADS_Output::register(
            'cnt',
            array(
                'all' => $chModel->setFilter(array())->countChanges()
            )            
        );        
        //ADS_Debug::dump(ads_prepare_paging($page, $chModel->getLimit(), $cnt));
        ADS_Output::register(
            'pager',
            ads_prepare_paging($page, $chModel->getLimit(), $cnt)
        );
    }
    
    public function handleCommitChanges() {        
        $deployment = new ADS_Deployment('0.0.2');        
        $dModel     = new ADS_DeploymentModel();  
        //load changes
        $chModel = new ADS_ChangelogModel();
        //count records
        $cnt = (int)$chModel->setFilter(array(
            array('status = %i', 0)
        ))->countChanges();
        if($cnt > 0){
            //create deployment       
            $dModel->addDeployment(array(
                'format'     => 'json',
                'swVersion'  => '0.0.2'
            ));
            //store descriptor
            $deploymentId = (int)dibi::getInsertId();
            $dModel->editDeployment($deploymentId, array(
                'descriptor' => $deployment->buildDescriptor('json', $deploymentId)
            ));
            //log change
            ADS_Logger::commitChanges($deploymentId);
        }
        //redir
        ADS_Navigator::redir('self:defaultAction',array(
            'msg' => self::STATUS_COMMIT_OK
        ));
    }
    
    public function handleStopPreview(){
        ADS_Output::renderOutput('Dashboard/stopPreview.html', TRUE); exit;
    }
}
